import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 章节类型
export interface Chapter {
  id?: number
  title: string
  content: string
  wordCount: number
  bookId: number
  order: number
  createTime?: string
  updateTime?: string
}

// 人物类型
export interface Character {
  id?: number
  name: string
  description: string
  avatar?: string
  bookId: number
  createTime?: string
  updateTime?: string
}

// 地点类型
export interface Location {
  id?: number
  name: string
  description: string
  bookId: number
  createTime?: string
  updateTime?: string
}

// 灵感类型
export interface Inspiration {
  id?: number
  title: string
  content: string
  bookId: number
  createTime?: string
  updateTime?: string
}

export const useBookDataStore = defineStore('bookData', () => {
  // 状态定义
  const chapters = ref<Chapter[]>([])
  const characters = ref<Character[]>([])
  const locations = ref<Location[]>([])
  const inspirations = ref<Inspiration[]>([])
  const currentBookId = ref<number | null>(null)
  
  // 当前选中的项目
  const currentChapterId = ref<number | null>(null)
  const currentCharacterId = ref<number | null>(null)
  const currentLocationId = ref<number | null>(null)
  const currentInspirationId = ref<number | null>(null)
  const currentSettingId = ref<number | null>(null)
  
  // 计算属性
  const hasChapters = computed(() => chapters.value.length > 0)
  const hasCharacters = computed(() => characters.value.length > 0)
  const hasLocations = computed(() => locations.value.length > 0)
  const hasInspirations = computed(() => inspirations.value.length > 0)
  
  const totalWordCount = computed(() => 
    chapters.value.reduce((total, chapter) => total + chapter.wordCount, 0)
  )
  
  const currentChapter = computed(() => 
    currentChapterId.value ? chapters.value.find(c => c.id === currentChapterId.value) : null
  )
  
  const currentCharacter = computed(() => 
    currentCharacterId.value ? characters.value.find(c => c.id === currentCharacterId.value) : null
  )
  
  const currentLocation = computed(() => 
    currentLocationId.value ? locations.value.find(l => l.id === currentLocationId.value) : null
  )
  
  const currentInspiration = computed(() => 
    currentInspirationId.value ? inspirations.value.find(i => i.id === currentInspirationId.value) : null
  )
  
  // 为了兼容IdeaLayout.vue中的使用方式，添加以下计算属性
  const currentOutline = ref(null)
  const currentCharacters = ref([])
  const currentLocations = ref([])
  const currentInspirations = ref([])
  const currentSettings = ref([])
  
  const characterTags = ref([])
  const inspirationCategories = ref([])
  
  // Actions
  function setBookId(bookId: number) {
    currentBookId.value = bookId
  }
  
  function setChapters(data: Chapter[]) {
    chapters.value = data
  }
  
  function setCharacters(data: Character[]) {
    characters.value = data
  }
  
  function setLocations(data: Location[]) {
    locations.value = data
  }
  
  function setInspirations(data: Inspiration[]) {
    inspirations.value = data
  }
  
  function addChapter(chapter: Chapter) {
    chapters.value.push(chapter)
  }
  
  function updateChapter(chapterId: number, updates: Partial<Chapter>) {
    const index = chapters.value.findIndex(c => c.id === chapterId)
    if (index !== -1) {
      chapters.value[index] = { ...chapters.value[index], ...updates }
    }
  }
  
  function removeChapter(chapterId: number) {
    const index = chapters.value.findIndex(c => c.id === chapterId)
    if (index !== -1) {
      chapters.value.splice(index, 1)
    }
  }
  
  function addCharacter(character: Character) {
    characters.value.push(character)
  }
  
  function updateCharacter(characterId: number, updates: Partial<Character>) {
    const index = characters.value.findIndex(c => c.id === characterId)
    if (index !== -1) {
      characters.value[index] = { ...characters.value[index], ...updates }
    }
  }
  
  function removeCharacter(characterId: number) {
    const index = characters.value.findIndex(c => c.id === characterId)
    if (index !== -1) {
      characters.value.splice(index, 1)
    }
  }
  
  function addLocation(location: Location) {
    locations.value.push(location)
  }
  
  function updateLocation(locationId: number, updates: Partial<Location>) {
    const index = locations.value.findIndex(l => l.id === locationId)
    if (index !== -1) {
      locations.value[index] = { ...locations.value[index], ...updates }
    }
  }
  
  function removeLocation(locationId: number) {
    const index = locations.value.findIndex(l => l.id === locationId)
    if (index !== -1) {
      locations.value.splice(index, 1)
    }
  }
  
  function addInspiration(inspiration: Inspiration) {
    inspirations.value.push(inspiration)
  }
  
  function updateInspiration(inspirationId: number, updates: Partial<Inspiration>) {
    const index = inspirations.value.findIndex(i => i.id === inspirationId)
    if (index !== -1) {
      inspirations.value[index] = { ...inspirations.value[index], ...updates }
    }
  }
  
  function removeInspiration(inspirationId: number) {
    const index = inspirations.value.findIndex(i => i.id === inspirationId)
    if (index !== -1) {
      inspirations.value.splice(index, 1)
    }
  }
  
  function clearAllData() {
    chapters.value = []
    characters.value = []
    locations.value = []
    inspirations.value = []
    currentBookId.value = null
    currentChapterId.value = null
    currentCharacterId.value = null
    currentLocationId.value = null
    currentInspirationId.value = null
    currentSettingId.value = null
  }
  
  // 设置当前选中的项目
  function setCurrentChapter(chapterId: number | null) {
    currentChapterId.value = chapterId
  }
  
  function setCurrentCharacter(characterId: number | null) {
    currentCharacterId.value = characterId
  }
  
  function setCurrentLocation(locationId: number | null) {
    currentLocationId.value = locationId
  }
  
  function setCurrentInspiration(inspirationId: number | null) {
    currentInspirationId.value = inspirationId
  }
  
  function setCurrentSetting(settingId: number | null) {
    currentSettingId.value = settingId
  }
  
  return {
    // 状态
    chapters,
    characters,
    locations,
    inspirations,
    currentBookId,
    
    // 当前选中项ID
    currentChapterId,
    currentCharacterId,
    currentLocationId,
    currentInspirationId,
    currentSettingId,
    
    // 计算属性
    hasChapters,
    hasCharacters,
    hasLocations,
    hasInspirations,
    totalWordCount,
    
    // 当前选中项
    currentChapter,
    currentCharacter,
    currentLocation,
    currentInspiration,
    
    // 兼容性属性
    currentOutline,
    currentCharacters,
    currentLocations,
    currentInspirations,
    currentSettings,
    characterTags,
    inspirationCategories,
    
    // 方法
    setBookId,
    setChapters,
    setCharacters,
    setLocations,
    setInspirations,
    addChapter,
    updateChapter,
    removeChapter,
    addCharacter,
    updateCharacter,
    removeCharacter,
    addLocation,
    updateLocation,
    removeLocation,
    addInspiration,
    updateInspiration,
    removeInspiration,
    clearAllData,
    setCurrentChapter,
    setCurrentCharacter,
    setCurrentLocation,
    setCurrentInspiration,
    setCurrentSetting
  }
})